#include "Form1.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;


Form1::Form1(void)
{
}

Form1::~Form1(void)
{
}

bool
Form1::Initialize()
{
	// Construct an XML form
	Construct(L"IDF_FORM1");

	return true;
}

result
Form1::OnInitializing(void)
{
	result r = E_SUCCESS;

	// TODO: Add your initialization code here

	// Get a button via resource ID
	__pButtonOk = static_cast<Button *>(GetControl(L"IDC_BUTTON_OK"));
	__pLabelPrecoGasolina = static_cast<Label *>(GetControl(L"IDC_LABEL_PRECO_GASOLINA"));
	__pLabelPrecoAlcool = static_cast<Label *>(GetControl(L"IDC_LABEL_PRECO_ALCOOL"));
	__pEntradaGasolina = static_cast<EditField *>(GetControl(L"IDC_EDITFIELD_GASOLINA_ENTRADA"));
	__pEntradaAlcool = static_cast<EditField *>(GetControl(L"IDC_EDITFIELD_ALCOOL_ENTRADA"));
	__pLabelResultadoGasolina = static_cast<Label *>(GetControl(L"IDC_LABEL_RESULTADO_GASOLINA"));
	__pLabelAlcool = static_cast<Label *>(GetControl(L"IDC_LABEL_RESULTADO"));

	__pLabelAlcool->SetShowState(false);
	__pLabelResultadoGasolina->SetShowState(false);

	if (__pButtonOk != null)
	{
		__pButtonOk->SetActionId(ID_BUTTON_OK);
		__pButtonOk->AddActionEventListener(*this);
	}

	return r;
}

result
Form1::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void
Form1::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	double gas;
	double alc;
	switch(actionId)
	{
	case ID_BUTTON_OK:
		{

			Double::Parse(__pEntradaGasolina->GetText(), gas);
			Double::Parse(__pEntradaAlcool->GetText(), alc);

			if(AlcoolEhMelhor(gas,alc)) {
				__pLabelResultadoGasolina->SetShowState(false);
				__pLabelAlcool->SetShowState(true);
				__pLabelAlcool->RequestRedraw(true);
			}else {
				__pLabelAlcool->SetEnabled(false);
				__pLabelResultadoGasolina->SetShowState(true);
				__pLabelResultadoGasolina->RequestRedraw(true);
			}
		}
		break;
	default:
		break;
	}
}

bool
Form1::AlcoolEhMelhor(double gasolinaValor, double alcoolValor) {
	bool r = E_SUCCESS;
	r = (alcoolValor + (gasolinaValor * 3)/10) < gasolinaValor;
	return r;
}

